package com.geh.learn.hutool;

import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.google.common.base.Charsets;
import com.google.common.io.BaseEncoding;

import javax.crypto.Cipher;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class EncryptRsaTest2 {

    public static final String publicKeyBase64 = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCHo/d5q6E7cvU2+dF7lS2hWB7V3sBlnidBXv4zP3R4MIC/yy5YjXQnb71YC/+7r8JU3FLOSCxt4P6JOWpz1wLf/8rwDSFye3DFqPfEl5yRpoWas3amu8PAXVvWwyKuOAC4jC433cdqeR5fNEwkhstnPbyQ5/1xZajuIxQuFCyIYQIDAQAB";

    public static final String privateKeyBase64 = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIej93mroTty9Tb50XuVLaFYHtXewGWeJ0Fe/jM/dHgwgL/LLliNdCdvvVgL/7uvwlTcUs5ILG3g/ok5anPXAt//yvANIXJ7cMWo98SXnJGmhZqzdqa7w8BdW9bDIq44ALiMLjfdx2p5Hl80TCSGy2c9vJDn/XFlqO4jFC4ULIhhAgMBAAECgYAA6dLuuv/zYdg6gBsH1FLF5m1SOr1UTdjlMvvemdm5nbd/OyG3M1Al/BXUAgQtRg8mocNtu80PShuirkwcBrWQbJbv0Y2TGamG1oFwzwMPt/g/TKC35Xf/8U5BMOX4PU/qOZse8V798MoD0J9c8YY5bUIR1ZGxL6ttkd5UGbObAQJBAPnVjvwn1dJwhSxLz4vHe+2g0mQa+gWa1CGAjpbFbM5ccX6RUFOWEatNGVvf7ME7qthQjaVPPxg15KQA5eOXD+ECQQCK/O/0gQKCbIwGUTVtXMznNjOEdk70gczXNKdT5fev0LSIWshK0dm14N/11NlkOheynKZ5hQseNvfG2QBQXoiBAkBtUOiTpMqGnf1neHSD8PFk9K2aZ8NmmGyhjCLYDeKiGQkAHI1NRajNj5q8/KoXkwgaRoT0BEzTRyGYMVxlzT+BAkBhRdL+cUf7VdLOHQniZ190tkdp/o8Vqr5dI5m0Ky+lWdDv2XFyg9n149N6rqQS9GFffSk5xTZZyCizf24r0W2BAkEA4FBbBbrOooRkMtryUHzHjOHaO8oxL7R/MYAO2FKK/Fo/NmZJXXohJBndxWqAQjsDjRZykvVg4mSaqV//lpr8DQ==";

    public static final RSA rsa = new RSA("RSA/ECB/PKCS1Padding", privateKeyBase64, publicKeyBase64);

    public static String encrypt(String original) throws Exception {
        // java默认"RSA"="RSA/ECB/PKCS1Padding"  加密处理
        X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(BaseEncoding.base64().decode(publicKeyBase64));
        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(pubKeySpec);
        Cipher cipherEncrypt = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipherEncrypt.init(Cipher.ENCRYPT_MODE, pubKey);
        return BaseEncoding.base64().encode(cipherEncrypt.doFinal(original.getBytes(Charsets.UTF_8)));
    }

    public static String decrypt(String encryptedString) throws Exception {
        PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(BaseEncoding.base64().decode(privateKeyBase64));
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(priKeySpec);
        Cipher cipherDecrypt = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipherDecrypt.init(Cipher.DECRYPT_MODE, priKey);
        return new String(cipherDecrypt.doFinal(BaseEncoding.base64().decode(encryptedString)), Charsets.UTF_8);
    }

    public static void main(String[] args) throws Exception {
        String original = "Hello World!";

        String encryptedHutool = rsa.encryptBase64(original, KeyType.PublicKey);
        System.out.println("Encrypt by hutool " + encryptedHutool);


        String decryptSelf = decrypt(encryptedHutool);
        System.out.println("Decrypt by self " + decryptSelf);


        // 交错验证
        String encryptedSelf = encrypt(original);
        System.out.println("Encrypt by self " + encryptedSelf);

        String decrypHutool = rsa.decryptStr(encryptedSelf, KeyType.PrivateKey);
        System.out.println("Decrypt by self " + decrypHutool);



    }
}
